
$(document).ready(function() {
	$('#stop').bind('click', function() {
		c_extension.sendMessage({"action": "stop"}, function(response) {
			//
		});
	});
	$('#refresh').bind('click', function() {
		c_extension.sendMessage({"action": "refresh"}, function(response) {
			window.close();
		});
	});
	$('#process').bind('click', function() {
		c_extension.sendMessage({"action": "stop"}, function(response) {
			alarms.clearAll();
			state.set(state.type.processing);
			c_extension.sendMessage({"action": "next"}, function(response) {
				window.close();
			});
		});
	});
	
	$( "#tabs" ).tabs({
		event: "mouseover"
	});
	$('#tabs').on('tabsactivate', function( event, ui ) { 
		if ($(ui.newPanel).attr('id') == 'tabs-1') {
			load.gifts();
		} else if ($(ui.newPanel).attr('id') == 'tabs-2') {
			load.requests();
		} else if ($(ui.newPanel).attr('id') == 'tabs-3') {
			load.joins();
		}
	});
	
	setInterval(function() {
		getNextRefresh();
	}, 500);
	
	getCount();
	getTabData();
	
	setInterval(function() { 
		getCount(); 
		//getTabData();
	}, 30000);
});

function getTabData() {
	load.gifts();
	load.requests();
	load.joins();
}

function getCount() {
	$.indexedDB(DB_NAME).objectStore("notification").count().then(function(val) { 
		$('#count').text('(' + val + ')');
	});
}

function getNextRefresh() {
	var s, m, h
	var alarm;
	
	c_alarms.getAll(function(arr) {
		//want to verify or reset the state if 
		//neccesary
		if (arr.length > 0) {
			for (var x in arr) {
				var alrmName = arr[x].name;
				if (alrmName == alarms.type.gather) {
					localStorage["process_state"] = state.type.gathering;
				} else if (alrmName == alarms.type.refresh) {
					localStorage["process_state"] = state.type.refresh;
				}
			}
		}
	});
	
	switch ((localStorage["process_state"]) ? Number(localStorage["process_state"]) : state.type.stopped) {
		case 0:
			$('#alarm').text('All Activity Stopped');
			break;
		case 1:
			$('#alarm').text('Processing...');
			break;
		case 2:
			$('#alarm').text('Gathering Links...');
			break;
		case 3:
		default:
			c_alarms.getAll(function(arr) { 
				if (arr.length > 0) { 
					for (var x in arr) {
						if (arr[x].name == 'AlarmRefresh') {
							alarm = arr[x];
							break;
						}
					}
				}
				
				if (alarm) {
					var ms = arr[0].scheduledTime - Date.now(); 
					s = Math.floor(((ms/1000)%60)); 
					m = Math.floor((((ms / 1000) / 60) % 60)); 
					h = Math.floor(((((ms / 1000) / 60) / 60) % 24)); 
					h = (h < 10) ? ('0' + h) : h; 
					m = (m < 10) ? ('0' + m) : m; 
					s = (s < 10) ? ('0' + s) : s; 
					
					$('#alarm').text('Next Refresh in - ' + h + ':' + m + ':' + s);
				}
			});
			break;
	}
}

var load = {
	"joins": function() {
		var joins = [];
		$('#joins').children().remove();
		$.indexedDB(DB_NAME).objectStore("Items").index("type").each(function(elm) { 
			joins.push({id: elm.value.gid});
		}, [itemType.crew]).done(function() { 
			var crewed = _.groupBy(joins, 'id');
			for (var i in crewed) {
				var name = (crew_items[i]) ? crew_items[i] : i;
				$('#joins').append('<tr><td>' + name + ' (' + crewed[i].length + ')</td></tr>');
			}
			$('#ui-id-3').text('Joins (' + joins.length + ')');
		});
	},
	"gifts": function() {
		var gifts = [];
		$('#gifts').children().remove();
		$.indexedDB(DB_NAME).objectStore("Items").index("type").each(function(elm) { 
			gifts.push({id: elm.value.gid}); 
		}, itemType.gift).done(function() { 
			var gift = _.groupBy(gifts, 'id'); 
			for (var i in gift) { 
				var name = (gift_items[i]) ? (gift_items[i]) : i;
				$('#gifts').append('<tr><td>' + name + ' (' + gift[i].length + ')</td></tr>');
			}
			$('#ui-id-1').text('Gifts (' + gifts.length + ')');
		});
	},
	"requests": function() {
		var reqs = [];
		$('#reqs').children().remove();
		$.indexedDB(DB_NAME).objectStore("Items").index("type").each(function(elm) { 
			reqs.push({id: elm.value.gid}); 
		}, itemType.request).done(function() { 
			var req = _.groupBy(reqs, 'id'); 
			for (var i in req) { 
				var name = (req_items[i]) ? (req_items[i]) : i;
				$('#reqs').append('<tr><td>' + name + ' (' + req[i].length + ')</td></tr>');
			}
			$('#ui-id-2').text('Requests (' + reqs.length + ')');
		});
	}
}